package com.example.second_home_work

import android.os.Bundle
import android.widget.Button
import android.widget.LinearLayout
import android.widget.ScrollView
import android.widget.Spinner
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    val expert = ProgramExpert()
    var cid=1
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val button1: Button = findViewById(R.id.button1)
        val button2: Button = findViewById(R.id.button2)
        val spinner: Spinner = findViewById(R.id.spinner)
        val textView: TextView = findViewById(R.id.textView)
        val scrollView: ScrollView = findViewById(R.id.scrollView)
        val linearLayout: LinearLayout = scrollView.findViewById(R.id.linearLayout1)
        button2.setOnClickListener{
            textView.text=expert.getAnswer(spinner.selectedItem.toString())
        }
        button1.setOnClickListener {
            if (cid > 10) {
                val newTextView = TextView(this)
                newTextView.text = "此学期修课过多"
                val layoutParams = LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT
                )
                layoutParams.setMargins(0, 16, 0, 0) // Set top margin to 16 pixels
                newTextView.layoutParams = layoutParams
                linearLayout.addView(newTextView)
                scrollView.fullScroll(ScrollView.FOCUS_DOWN)
            } else {
                val newstu = "选课成功，课程号为: CS11${cid++}"
                val newTextView = TextView(this)
                newTextView.text = newstu
                val layoutParams = LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT
                )
                layoutParams.setMargins(0, 16, 0, 0) // Set top margin to 16 pixels
                newTextView.layoutParams = layoutParams
                linearLayout.addView(newTextView)
                scrollView.fullScroll(ScrollView.FOCUS_DOWN)
            }
        }
    }
}